package assignment1;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.text.*;

@SuppressWarnings("serial")
public class MyControlPanel extends JPanel
{   //This class is used to add components and methods to the MyFrame class.
    //Creating the components that will need to be accessed by other classes.
    JSlider jSlider1; // used to define the size of the shape.
    JTextField jTextField1; // used to display the length of the shape
    JTextField jTextField2; // used to display the area of the shape
    static MyShape myShape; // used to define which calculations are used.
    static DrawShape shape; // create a DrawShape panel to display the shape.
    static int shapeSize; // used to define the size of the shape.

    public void startControl()
    {   /*this method will be used in the MyFrame class to create an instance
        of the MyControlPanel.*/
        
        /*create an instance of a JSlider with tick labels, an initial value of 
        50 and add a changer listener too regisiter the current value.*/
        jSlider1 = new JSlider();
        jSlider1.setMajorTickSpacing(10);
        jSlider1.setPaintLabels(true);
        jSlider1.setValue(50);
        jSlider1.addChangeListener(new MyChangeAction());
        
        //create labels to inform the user of the values.
        JLabel jLabel1 = new JLabel("Boundary Length");
        JLabel jLabel2 = new JLabel("Area");
        JLabel jLabel3 = new JLabel("Shape Dimension");
        
        /*create instances of the jTextFields that will be used to output
        the area and length information.*/
        jTextField1 = new JTextField(6);
        jTextField2 = new JTextField(6);
        
        //add all of the components created above to the panel.
        this.add(jSlider1);
        this.add(jLabel1);
        this.add(jLabel2);
        this.add(jTextField1);
        this.add(jLabel3);
        this.add(jTextField2);

        /*I created my GroupLayout using a Swing GUI form within NetBeans. 
        This was a drag and drop method, I then copied and pasted the 
        code into this class so that I could amended it when required.*/
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(54, 54, 54)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(61, 61, 61)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 93, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(64, 64, 64)));

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(326, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)))
                .addGap(21, 21, 21)));
     }
        //create an instance of the MyChangeAction listener defined below.
        MyChangeAction sliderAction = new MyChangeAction();
        
        public class MyChangeAction implements ChangeListener
        {   /*This ChangeListener will be used to record the value of the
             *JSlider. It will pass the area and length values to the
             JTextFields. It will also define the size of the shape and will
             repaint the shape each time the jSlider is moved, creating a
             simple animation.*/
            @Override
            public void stateChanged(ChangeEvent e)
                {   /*create an instance of myShape and make it the same as the
                    MyFrame value as the MenuItems that change the shape used 
                    are in the MyFrame class.*/
                    myShape = MyFrame.myShape;
                    //make shapeSize in DrawShape equal the value of the slider.
                    DrawShape.shapeSize = jSlider1.getValue();
                    //create a one decimal point format to be used for values. 
                    DecimalFormat df = new DecimalFormat("#.#");
                    /*Set the jTextField values to the area and length of the
                     shape chosen. This is done by changing the string to a 
                     numerical value, then passing this value to the myShape
                     in order to calculate the values. The DecimalFormat is 
                     also used to keep it to one decimal point.*/
                    jTextField1.setText(String.valueOf(df.format(myShape.calcLength(jSlider1.getValue()))));
                    jTextField2.setText(String.valueOf(df.format(myShape.calcArea(jSlider1.getValue()))));
                    MyFrame.pane.repaint(); //this repaints the pane each time the slider is moved.
                }
        }
        
        public void linkEventHandler()
        {   //this adds the ChangeListener to the jSlider.
            jSlider1.addChangeListener(sliderAction);
        } 
}